#requires –version 2.0

function Update-VmTools {
	param
(
		[Parameter(Position=0,Mandatory=$true)][string]$ComputerName,
		[Parameter(Mandatory=$true)][string]$SubScripts,
		[Parameter(Mandatory=$false)][switch]$StayConnected,
		[Parameter(Mandatory=$true)][string]$vCenter,
		[Parameter(Mandatory=$false)][boolean]$UseAltViCredsBool = $false,
		[Parameter(Mandatory=$false)]$ViCreds
	)
	$ErrorActionPreference = "inquire"
	
	# VARIABLES
	[string]$Notes = ''
	[boolean]$Success = $false
	[datetime]$SubStartTime = Get-Date
	[string]$ComputerName = $ComputerName.ToUpper()
	
	[boolean]$VmFound = $false
	[boolean]$updatetriggered = $false
	$task = $null
	$id = $null
	$taskstatus = $null
#	$errors = $null
	
	. "$SubScripts\Func_Connect-ViHost_1.0.7.ps1"
	. "$SubScripts\Func_Disconnect-ViHost_1.0.1.ps1"
	. "$SubScripts\Func_Get-Runtime_1.0.3.ps1"
		
	if ($Global:UpdateVmTools) {
		Remove-Variable UpdateVmTools -Scope "Global"
	}
	
	#region Tasks
		
		if ($UseAltViCredsBool -eq $true) {
			if ($ViCreds) {
				Connect-VIHost -ViHost $vCenter -SubScripts $SubScripts -AltViCreds -ViCreds $ViCreds
			}
			else
{
				Connect-VIHost -ViHost $vCenter -SubScripts $SubScripts -AltViCreds
			}
		}
		else
{
			Connect-VIHost -ViHost $vCenter -SubScripts $SubScripts
		}
		
		if ($Global:ConnectViHost.VIConnect -eq $true) {
			# Get Virtual Machines object information
			Try {
#				$VMGuest = Get-VMGuest $ComputerName -ErrorAction Stop #Does it by Guest Operating System
				$GetVM = Get-VM -Name $ComputerName -ErrorAction Stop
				[boolean]$VmFound = $true
			}
			Catch {
				[boolean]$VmFound = $false
				[string]$Notes += 'Get-VM Failed '
			}
			# Continue if VM was found
			if ($VmFound -eq $true) {
				Try {
					# TRIGGER UPDATE TOOLS WITHOUT REBOOT ALLOWED
#					$task = $VMGuest | Update-Tools -NoReboot -RunAsync -ErrorAction Stop #Does it by Guest Operating System
#					$task = Update-Tools -VM $ComputerName -NoReboot -RunAsync -ErrorAction Stop
					# -ErrorVariable $updateerror
					$task = $GetVM | Update-Tools -NoReboot -RunAsync -ErrorAction Stop
#					$VMGuest | Update-Tools -NoReboot -ErrorAction Stop #05/02/2012
#					$task = Update-Tools -VM $GetVM -NoReboot -RunAsync -ErrorAction Stop #fails 4/20/2012 say key added error, but runs
					[boolean]$updatetriggered = $true
				}
				Catch {
					[string]$Notes += 'Update-Tools Command Failed '
#					$errors += $updateerror
					[boolean]$updatetriggered = $false
				}
				# If tools update was triggered pull associated task and wait for completion
				if ($updatetriggered -eq $true) {
					$id = $task.Id
					Wait-Task -Task $task
					$taskstatus = Get-Task | Where-Object {$_.Id -eq $id}
					
					if ($taskstatus -ne $null) {
						$taskstate = $taskstatus.State
					}
					else
{
						[string]$Notes += 'ERROR: Get-Task Issue '
					}
					
					if ($taskstate -eq "Success") {
						[boolean]$Success = $true
						[string]$Notes += 'Completed '
					}
					Elseif ($taskstate -eq "Error") {
						[string]$Notes += 'ERROR: Task State is Error '
					}
					else
{
						[string]$Notes += 'ERROR: Task State not Success or Error'
					}
				}
			}
			else
{
				[string]$Notes += 'Failed to find VM '
			}
		}
		else
{
			[string]$Notes += 'ViHost Connection Failed '
		}
		if ($StayConnected.IsPresent -eq $false) {
			Disconnect-VIHost	
		}
		
	#endregion Tasks
	
	# Determine Success ^Done on line 84
#	if ($taskstate -eq "Success") {
#		[boolean]$Success = $true
#	}
	
	Get-Runtime -StartTime $SubStartTime
	# Create Results Custom PS Object
	$Global:UpdateVmTools = New-Object -TypeName PSObject -Property @{
		ComputerName = $ComputerName
		Notes = $Notes
		Success = $Success
		Starttime = $SubStartTime
		Endtime = $Global:GetRunTime.Endtime
		Runtime = $Global:GetRunTime.Runtime
		vCenter = $vCenter
		ViConnect = $Global:ConnectViHost.VIConnect
#		Errors = $errors
	}
}

#region Notes

<# Description
	function to upgrade Vmware Tools on VM Guest using PowerCLI snapin.
#>

<# Author
	Levon Becker
	PowerShell.Guru@BonusBits.com
	http://wiki.bonusbits.com
#>

<# Dependents
	Func_Run-Patching.ps1
#>

<# Dependencies
	Func_Get-Runtime
	Func_Connect-ViHost
	Func_Disconnect-ViHost
#>

<# Change Log
	1.000 - 02/15/2011 (Beta)
		Created
	1.001 - 05/06/2011
		Adding PSObject Output
		Added Runtime recording
	1.002 - 05/13/2011
		Rename Result PSObject to UpdateVmTools (Was GetIPConfig still)
	1.003 - 10/02/2011
		Changed task variable names
		Added function success condition to end
		Fixed Update command to include Get-Vmguest $ComputerName | (Fixed the failures)
		Added error handling for if VM is not found.
	1.004 - 10/11/2011
		Change Get-Vmguest to Get-VM, seemed to fix the failures
	1.005 - 11/10/2011
		Added more parameter settings
		Added $UseAltViCredsBool and $ViCreds
	1.006 - 11/11/2011
		Changed to use Func_Connect-ViHost
	1.0.7 - 04/20/2012
		Renamed ComputerName to computername
		Changed StayConnected to switch
#>

<# To Do List

#>

<# Sources

#>

#endregion Notes
